import { useRef, useState, useEffect } from 'react';
import type { Dispatch, SetStateAction, MutableRefObject } from 'react';

/**
 * 同时存储state和ref
 */
export function useStateRef<T>(initialValue: T): [T, Dispatch<SetStateAction<T>>, MutableRefObject<T>] {
  const [value, setValue] = useState<T>(initialValue);
  const ref = useRef<T>(value);
  useEffect(() => {
    ref.current = value;
  }, [value]);
  return [value, setValue, ref];
}
